package com.yh.zz_hlj.modules.adatper;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yh.zz_hlj.AppContext;
import com.yh.zz_hlj.R;
import com.yh.zz_hlj.base.BaseFragment;
import com.yh.zz_hlj.common.PLog;
import com.yh.zz_hlj.common.UIHelper;
import com.yh.zz_hlj.modules.bean.HeaderBean;
import com.yh.zz_hlj.modules.bean.SimpleBackPage;
import com.yh.zz_hlj.modules.bean.User;
import com.yh.zz_hlj.modules.bean.WorkInfoSalary;
import com.yh.zz_hlj.modules.bean.WorkSalaryBean;
import com.yh.zz_hlj.modules.bean.t_my_pgdanEntry01;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p/>
 * 工程：ZWSR
 * <p/>
 * 文件名：SalaryReportAdapter
 * <p/>
 * 创建时间：2016.10.25.14:30
 * *****************************
 */
public class SalaryReportAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static String TAG = SalaryReportAdapter.class.getSimpleName();
    private BaseFragment fragment;

    private FragmentActivity mContext;
    private final int TYPE_ONE = 0;
    private final int TYPE_TWO = 1;
    private final int TYPE_THREE = 2;
    private final int TYPE_FORE = 3;
    private WorkSalaryBean workBean;
    private DecimalFormat formartDouble;
    private double price = 0;
    private int count_all = 0;
    private TextView tv_count_all_left;
    public boolean isDYCountAll = false;
    private List<Integer> mHeights;
    private double currentCheck;
    private boolean isChanged;
    private String startDate;
    private String endDate;

    public WorkSalaryBean getWorkBean() {
        return workBean;
    }

    public SalaryReportAdapter(BaseFragment fragment, FragmentActivity context, WorkSalaryBean workBean, String startDate, String endDate) {
        if (formartDouble == null) {
            formartDouble = new DecimalFormat("#.00");
        }
        this.fragment = fragment;
        this.mContext = context;
        this.workBean = workBean;
        this.startDate=startDate;
        this.endDate=endDate;

    }

    @Override
    public int getItemViewType(int position) {
        if (position == TYPE_ONE) {
            return TYPE_ONE;
        }
        if (position == TYPE_TWO) {
            return TYPE_TWO;
        }
        if (position == TYPE_THREE) {
            return TYPE_THREE;
        }
        if (position == TYPE_FORE) {
            return TYPE_FORE;
        }
        return super.getItemViewType(position);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ONE) {
            return new HeaderViewHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.content_salary_header, parent, false));
        }
        if (viewType == TYPE_TWO) {
            return new WorkerEntryViewHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_hour_info, parent, false));
        }
        if (viewType == TYPE_THREE) {
            return new HeaderTitileViewHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_salaryreport_header, parent, false));
        }
        if (viewType == TYPE_FORE) {
            return new WorkEntryViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_hour_info, parent, false));
        }
        return null;
    }

    private void changeCurrentCheck(boolean isChecked, int position) {
        double fjsValue = workBean.getWorkInfos().get(position).getFJSValue();
        if (isChecked) {
            currentCheck = add(currentCheck, fjsValue);
            tv_count_all_left.setText(currentCheck + workBean.getHeaderBean().getfJSName());
        } else {
            currentCheck = sub(currentCheck, fjsValue);
            tv_count_all_left.setText(currentCheck + workBean.getHeaderBean().getfJSName());
        }
    }

    private void changeUI(int count_all, boolean isChecked, int position) {

        String div = div(currentCheck, (double) workBean.getWorkusers().size(), 2);
        for (int i = 0; i < workBean.getWorkusers().size(); i++) {
            workBean.getWorkusers().get(i).setFAmount(Double.parseDouble(div));
        }

        for (int i = 0; i < workBean.getWorkusers().size(); i++) {
            mNum[i].setText(div);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof WorkEntryViewHolder) {
            try {
                ArrayList<WorkInfoSalary> workInfos = workBean.getWorkInfos();
                for (int i = 0; i < workInfos.size(); i++) {
                    //s.subString(s.length-3,s.length);
                    //第一个参数是开始截取的位置，第二个是结束位置。
                    final WorkInfoSalary mDate = workInfos.get(i);
                    ((WorkEntryViewHolder) holder).mClock[i].setText(
                            mDate.getFBillNo());
                    ((WorkEntryViewHolder) holder).mTemp[i].setText(mDate.getFJSValue()+"");
                    ((WorkEntryViewHolder) holder).mHumidity[i].setText(
                            mDate.getLeftAmount()+"");
                    if (mDate.getOutFWages()) {
                        ((WorkEntryViewHolder) holder).mWindShow[i].setVisibility(View.VISIBLE);
                        ((WorkEntryViewHolder) holder).mWind[i].setVisibility(View.GONE);
                    } else {
                        if (mDate.getLeftAmount() != 0) {
                            ((WorkEntryViewHolder) holder).mWindShow[i].setVisibility(View.VISIBLE);
                            ((WorkEntryViewHolder) holder).mWind[i].setVisibility(View.GONE);
                            ((WorkEntryViewHolder) holder).mWindShow[i].setText("未完成");
                        } else {
                            ((WorkEntryViewHolder) holder).mWindShow[i].setVisibility(View.GONE);
                            ((WorkEntryViewHolder) holder).mWind[i].setVisibility(View.VISIBLE);
                            final int finalI = i;
                            ((WorkEntryViewHolder) holder).mWind[i].setChecked(true);
                            ((WorkEntryViewHolder) holder).mWind[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                    mDate.setFWages(isChecked);
                                    isChanged=true;
                                    if (isChecked) {
                                        count_all++;
                                        changeCurrentCheck(isChecked, finalI);
                                    } else {
                                        count_all--;
                                        changeCurrentCheck(isChecked, finalI);
                                    }
                                    changeUI(count_all, isChecked, finalI);
                                }
                            });

                        }
                    }
                    if (!mDate.getOutFWages()) {
                        mDate.setFWages(true);
                    }else {
                        mDate.setFWages(false);
                    }
                }
            } catch (Exception e) {
                //Snackbar.make(holder.itemView, R.string.api_error, Snackbar.LENGTH_SHORT).show();
                PLog.e(TAG, e.toString());
            }
        } else if (holder instanceof HeaderViewHolder) {
            HeaderBean headerBean = workBean.getHeaderBean();
            try {
                ((HeaderViewHolder) holder).tv_work_hbuser.setText(headerBean.getHbuser());
                ((HeaderViewHolder) holder).tv_work_hbcjgx.setText(
                        headerBean.getHbcjgx());
                ((HeaderViewHolder) holder).tv_work_hbtime.setText(
                        headerBean.getReportTime());
                ((HeaderViewHolder) holder).tv_report_date.setText(
                        startDate+"——"+endDate);

                ArrayList<WorkInfoSalary> mminfos = workBean.getWorkInfos();
                double doneCount = workBean.getHeaderBean().getFJSValue_Done(), noDoneCount = 0, allCount = 0;
//                for (int k = 0; k < mminfos.size(); k++) {
//                    WorkInfoSalary mmmmminfo = mminfos.get(k);
//                    allCount += workBean.getHeaderBean().getOneValue();
//                    if (mmmmminfo.getOutFwgtype()) {
//                        doneCount += workBean.getHeaderBean().getOneValue();
//                    }
//                }
                ((HeaderViewHolder) holder).tv_count_all_done.setText(
                        formartDouble.format(doneCount) + workBean.getHeaderBean().getfJSName());
                ((HeaderViewHolder) holder).tv_count_all.setText(
                        workBean.getHeaderBean().getCount_all() + workBean.getHeaderBean().getfJSName());

//                double mmCount = count_all * (workBean.getHeaderBean().getOneValue());
                ArrayList<WorkInfoSalary> workInfos = workBean.getWorkInfos();
                if (!isChanged) {
                    currentCheck = 0d;
                    for (int i = 0; i < workInfos.size(); i++) {
                        if (!workInfos.get(i).getOutFWages()) {
                            currentCheck = add(currentCheck, workInfos.get(i).getFJSValue());
                        }
                    }
                    tv_count_all_left.setText(currentCheck + workBean.getHeaderBean().getfJSName());
                }
                ArrayList<t_my_pgdanEntry01> infos = workBean.getWorkusers();
                String newName = "";


                if (infos != null) {
                    for (int i = 0; i < infos.size(); i++) {
                        if (i == infos.size() - 1) {
                            newName += "{" + infos.get(i).getFHBUserName() + "}";
                        } else {
                            newName += "{" + infos.get(i).getFHBUserName() + "}" + "、";
                        }
                    }
                }
                ((HeaderViewHolder) holder).tv_users_all.setText(
                        newName);
            } catch (Exception e) {
                //Snackbar.make(holder.itemView, R.string.api_error, Snackbar.LENGTH_SHORT).show();
                PLog.e(TAG, e.toString());
            }
        } else if (holder instanceof WorkerEntryViewHolder) {
            userAction();
        }
    }

    private void userAction() {
        final ArrayList<t_my_pgdanEntry01> workusers = workBean.getWorkusers();
        init_WorkerEntryViewHolder();
//        double mmCount = count_all * (workBean.getHeaderBean().getOneValue());
        final String averageNum = div(currentCheck, (double) workusers.size(), 2);
        for (int i = 0; i < workusers.size(); i++) {
            final t_my_pgdanEntry01 t_my_pgdanEntry01 = workusers.get(i);
            mPirce[i].setText(price + "");
            mName[i].setText(t_my_pgdanEntry01.getFHBUserName());
            mNum[i].setText(averageNum);
            mMoney[i].setText("0.0");
            t_my_pgdanEntry01.setFPrice(price);
            t_my_pgdanEntry01.setFAmount(Double.parseDouble(mNum[i].getText().toString().trim()));
            final int finalI = i;
            double price = Double.parseDouble(mPirce[i].getText().toString().trim());
            double all_money = Double.parseDouble(averageNum) * price;
            if (all_money == 0) {
                mMoney[i].setText("0.0");
                t_my_pgdanEntry01.setFMoney(0);
            } else {
                mMoney[i].setText(formartDouble.format(all_money) + "");
                t_my_pgdanEntry01.setFMoney(all_money);
            }

            final int finalI1 = i;
            mNum[i].addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    String strPrice = mPirce[finalI].getText().toString().trim();
                    if (!".".equals(s.toString())) {
                        if (!TextUtils.isEmpty(s.toString())) {
                            if (!TextUtils.isEmpty(strPrice)) {
                                double price = Double.parseDouble(strPrice);
                                double num = Double.parseDouble(s.toString());
                                double all_money = price * num;
                                if (all_money == 0) {
                                    mMoney[finalI].setText("0.0");
                                    t_my_pgdanEntry01.setFMoney(0);
                                    t_my_pgdanEntry01.setFAmount(0);
                                    t_my_pgdanEntry01.setFPrice(price);
                                } else {
                                    String format = formartDouble.format(all_money);
                                    mMoney[finalI].setText(format);
                                    t_my_pgdanEntry01.setFMoney(Double.parseDouble(format));
                                    t_my_pgdanEntry01.setFAmount(num);
                                    t_my_pgdanEntry01.setFPrice(price);
                                }
                            }
                        } else {
                            t_my_pgdanEntry01.setFMoney(0);
                            t_my_pgdanEntry01.setFAmount(0);
                            t_my_pgdanEntry01.setFPrice(0);
                            mMoney[finalI].setText("");
                        }
                    } else {
                        mNum[finalI].setText("");
                        mMoney[finalI].setText("");
                        t_my_pgdanEntry01.setFMoney(0);
                        t_my_pgdanEntry01.setFAmount(0);
                        t_my_pgdanEntry01.setFPrice(0);
                    }

                    if (!TextUtils.isEmpty(s.toString()) && !".".equals(s.toString())) {
                        double mmv = Double.parseDouble(s.toString());
                        for (int j = 0; j < workusers.size(); j++) {
                            if (finalI != j) {
                                mmv += workusers.get(j).getFAmount();
                            }
                        }
                        String allCountStr = tv_count_all_left.getText().toString().replace(workBean.getHeaderBean().getfJSName(), "").trim();
                        mmv = Double.parseDouble(formartDouble.format(mmv));
                        if (mmv > Double.parseDouble(allCountStr)) {
                            isDYCountAll = true;
                            Toast.makeText(mContext, workBean.getHeaderBean().getfJSName() + "总和不能大于:" + allCountStr, Toast.LENGTH_SHORT).show();
                        } else {
                            isDYCountAll = false;
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {


                }
            });

            mPirce[i].addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    String strCount = mNum[finalI].getText().toString().trim();
                    if (!".".equals(s.toString())) {
                        if (!TextUtils.isEmpty(s.toString())) {
                            if (!TextUtils.isEmpty(strCount)) {
                                double price = Double.parseDouble(s.toString());
                                double num = Double.parseDouble(strCount);
                                double all_money = price * num;
                                if (all_money == 0) {
                                    mMoney[finalI].setText("0.0");
                                    t_my_pgdanEntry01.setFMoney(0);
                                    t_my_pgdanEntry01.setFAmount(0);
                                    t_my_pgdanEntry01.setFPrice(0);
                                } else {
                                    String format = formartDouble.format(all_money);
                                    mMoney[finalI].setText(format);
                                    t_my_pgdanEntry01.setFMoney(Double.parseDouble(format));
                                    t_my_pgdanEntry01.setFAmount(num);
                                    t_my_pgdanEntry01.setFPrice(price);
                                }
                            }
                        } else {
                            t_my_pgdanEntry01.setFMoney(0);
                            t_my_pgdanEntry01.setFAmount(0);
                            mMoney[finalI].setText("");
                            t_my_pgdanEntry01.setFPrice(0);
                        }
                    } else {
                        mNum[finalI].setText("");
                        mMoney[finalI].setText("");
                        t_my_pgdanEntry01.setFMoney(0);
                        t_my_pgdanEntry01.setFAmount(0);
                        t_my_pgdanEntry01.setFPrice(0);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {


                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    /**
     * 顶部
     */
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        private CardView cardView;
        private TextView tv_work_hbuser;
        private TextView tv_work_hbcjgx;
        private TextView tv_work_hbtime;
        private TextView tv_count_all;
        private TextView tv_users_all;
        private TextView tv_count_all_done;
        private TextView tv_report_date;
        LinearLayout ll_users;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.cardView);
            tv_report_date = (TextView) itemView.findViewById(R.id.tv_report_date);
            tv_work_hbuser = (TextView) itemView.findViewById(R.id.tv_work_hbuser);
            tv_work_hbcjgx = (TextView) itemView.findViewById(R.id.tv_work_hbcjgx);
            tv_work_hbtime = (TextView) itemView.findViewById(R.id.tv_work_hbtime);
            tv_count_all_left = (TextView) itemView.findViewById(R.id.tv_count_all_left);
            tv_count_all_done = (TextView) itemView.findViewById(R.id.tv_count_all_done);
            tv_count_all = (TextView) itemView.findViewById(R.id.tv_count_all);
            tv_users_all = (TextView) itemView.findViewById(R.id.tv_users_all);
            ll_users = (LinearLayout) itemView.findViewById(R.id.ll_users);
            ll_users.setVisibility(View.VISIBLE);
            ll_users.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("type", "select");
                    UIHelper.showSimpleBackForResult(fragment, mContext, 10, SimpleBackPage.TEAM, bundle, "选择工人");
                }
            });
        }
    }


    /**
     * 顶部
     */
    class HeaderTitileViewHolder extends RecyclerView.ViewHolder {
        private TextView one_clock;
        private TextView one_temp;
        private TextView one_humidity;
        private TextView one_wind;



        public HeaderTitileViewHolder(View itemView) {
            super(itemView);
            one_clock = (TextView) itemView.findViewById(R.id.one_clock);
            one_temp = (TextView) itemView.findViewById(R.id.tv_work_hbuser);
            one_humidity = (TextView) itemView.findViewById(R.id.one_humidity);
            one_wind = (TextView) itemView.findViewById(R.id.one_wind);

        }
    }

    /**
     * 人员
     */

    private ImageView[] mDelete;
    private EditText[] mNum;
    private EditText[] mPirce;
    private TextView[] mMoney;
    private TextView[] mName;
    private TextView[] mJSDW;

    private void init_WorkerEntryViewHolder() {
        User mUser = AppContext.getInstance().getCurrentUser();
        if (mUser != null) {
            price = mUser.getFgongjia();
        }
        itemHourInfoLinearlayout.removeAllViews();
        mDelete = new ImageView[workBean.getWorkusers().size()];
        mNum = new EditText[workBean.getWorkusers().size()];
        mPirce = new EditText[workBean.getWorkusers().size()];
        mMoney = new TextView[workBean.getWorkusers().size()];
        mName = new TextView[workBean.getWorkusers().size()];
        mJSDW = new TextView[workBean.getWorkusers().size()];
        for (int i = 0; i < workBean.getWorkusers().size(); i++) {
            final View view = View.inflate(mContext, R.layout.item_workentry_detail, null);
            mName[i] = (TextView) view.findViewById(R.id.tv_name);
            mDelete[i] = (ImageView) view.findViewById(R.id.img_delete);
            mJSDW[i] = (TextView) view.findViewById(R.id.tv_jsdw);
            final int finalI = i;
            mDelete[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteWorkerItem(finalI, view);
                }
            });
            mNum[i] = (EditText) view.findViewById(R.id.et_num);
            mNum[i].setHint(workBean.getHeaderBean().getfJSName());
            mJSDW[i].setHint(workBean.getHeaderBean().getfJSName());
            mPirce[i] = (EditText) view.findViewById(R.id.et_price);
            mMoney[i] = (TextView) view.findViewById(R.id.et_money);

            final int finalI1 = i;
            mNum[i].setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        mNum[finalI1].setText("");
                    } else {
                    }
                }
            });
            mPirce[i].setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        mPirce[finalI1].setText("");
                    } else {
                    }
                }
            });
            itemHourInfoLinearlayout.addView(view);
        }
    }

    private LinearLayout itemHourInfoLinearlayout;

    class WorkerEntryViewHolder extends RecyclerView.ViewHolder {
        ArrayList<t_my_pgdanEntry01> workInfos = workBean.getWorkusers();

//        private ImageView[] mDelete = new ImageView[workInfos.size()];
//        private EditText[] mNum = new EditText[workInfos.size()];
//        private TextView[] mPirce = new TextView[workInfos.size()];
//        private TextView[] mMoney = new TextView[workInfos.size()];
//        private TextView[] mName = new TextView[workInfos.size()];

        public WorkerEntryViewHolder(View itemView) {
            super(itemView);
            itemHourInfoLinearlayout = (LinearLayout) itemView.findViewById(R.id.item_hour_info_linearlayout);
//                for (int i = 0; i < workInfos.size(); i++) {
//                    View view = View.inflate(mContext, R.layout.item_workentry_detail, null);
//                    mName[i] = (TextView) view.findViewById(R.id.tv_name);
//                    mDelete[i] = (ImageView) view.findViewById(R.id.img_delete);
//                    mNum[i] = (EditText) view.findViewById(R.id.et_num);
//                    mPirce[i] = (TextView) view.findViewById(R.id.et_price);
//                    mMoney[i] = (TextView) view.findViewById(R.id.et_money);
//                    itemHourInfoLinearlayout.addView(view);
//                }
        }
    }

    private void deleteWorkerItem(int finalI, View v) {
        workBean.getWorkusers().remove(finalI);
        TranslateAnimation ta = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 1,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0);
        ta.setDuration(300);
//        ta.setFillAfter(true);
        v.startAnimation(ta);
        ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                notifyDataSetChanged();
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


    }

    /**
     * 工序详情
     */
    class WorkEntryViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout itemHourInfoLinearlayout;
        ArrayList<WorkInfoSalary> workInfos = workBean.getWorkInfos();
        private TextView[] mClock = new TextView[workInfos.size()];
        private TextView[] mTemp = new TextView[workInfos.size()];
        private TextView[] mHumidity = new TextView[workInfos.size()];
        private CheckBox[] mWind = new CheckBox[workInfos.size()];
        private TextView[] mWindShow = new TextView[workInfos.size()];

        public WorkEntryViewHolder(View itemView) {
            super(itemView);
            itemHourInfoLinearlayout = (LinearLayout) itemView.findViewById(R.id.item_hour_info_linearlayout);

            for (int i = 0; i < workInfos.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_salaryentry_line, null);
                mClock[i] = (TextView) view.findViewById(R.id.one_clock);
                mTemp[i] = (TextView) view.findViewById(R.id.one_temp);
                mHumidity[i] = (TextView) view.findViewById(R.id.one_humidity);
                mWind[i] = (CheckBox) view.findViewById(R.id.one_wind);
                mWindShow[i] = (TextView) view.findViewById(R.id.one_wind_show);

                itemHourInfoLinearlayout.addView(view);
            }
        }
    }

    /**
     * 工具
     * 返回两个double类型相除并保留两位小数
     *
     * @param v1
     * @param v2
     * @param scale
     * @return
     */
    public String div(Double v1, Double v2, int scale) {
        BigDecimal b1 = null;
        BigDecimal b2 = null;
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        if (v2 != 0) {
            b1 = new BigDecimal(v1.toString());
            b2 = new BigDecimal(v2.toString());
            return b1.divide(b2, scale, BigDecimal.ROUND_DOWN).doubleValue() + "";
        } else {
            return "";
        }
    }

    /**
     * 提供精确的加法运算。
     *
     * @param v1 被加数
     * @param v2 加数
     * @return 两个参数的和
     */
    public double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    /**
     * 提供精确的减法运算。
     *
     * @param v1 被减数
     * @param v2 减数
     * @return 两个参数的差
     */

    public double sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }
}
